// pages/phone/changephone/changephone.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber:"",
    verificationCode:'',
    bindusername:"",
    code:''

  },

  // 获取验证码
  getCode: function () {
    const data = {
      phoneNumber: this.data.phoneNumber,
      verificationCode: this.data.verificationCode
    };
    
    // 将 JSON 对象转换为字符串
    const jsonData = JSON.stringify(data);
    const that = this;

    if (this.data.isCounting) return;

    wx.request({
      url:'http://localhost:8080/api/getCode',
      method:'POST',
      data:{
        phoneNumber: this.data.phoneNumber
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'  
      },
      success(res) {
        wx.showToast({
          title: '验证码已发送',
          icon: 'none'
        });

        // 开始倒计时
        that.startCountDown();
      }
    });
  },

  // 开始倒计时
  startCountDown: function () {
    const that = this;
    this.setData({
      isCounting: true,
      codeText: `重新获取 (${this.data.countDown}s)`
    });

    const interval = setInterval(() => {
      let count = that.data.countDown - 1;
      if (count <= 0) {
        clearInterval(interval);
        that.setData({
          isCounting: false,
          codeText: '获取验证码',
          countDown: 60
        });
      } else {
        that.setData({
          countDown: count,
          codeText: `重新获取 (${count}s)`
        });
      }
    }, 1000);
  },

  // 验证验证码并跳转
  verifyCode: function () {
    let that = this;

      wx.request({
        url: 'http://localhost:8080/api/verifyCode',
        method: 'POST',
        data: {
          phoneNumber: this.data.phoneNumber,
          code: this.data.verificationCode
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'  
        },
        success(res) {
          console.log(res);
          //更改数据库中的相关用户的手机号
          wx.request({
            url: 'http://localhost:8080/user/updatePhoneByName',
            method:'POST',
            data:{
              username:that.data.username,
              phone:that.data.phoneNumber
            },
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'  
            },
            success(res){
              console.log(res);
              console.log("成功更新");
              // 修改全局变量的值
              console.log(that.data.phoneNumber);
            
              app.globalData.username = that.data.phoneNumber;
              console.log(" 修改全局变量的值");
              console.log(app.globalData.username);
            }
          })
          if (res.data) {
            wx.navigateTo({
              url: '/pages/phone/successP/successP',
            })
          } else {
            wx.showToast({
              title: '验证码错误或已过期',
              icon: 'none'
            });
          }
          //更改数据库中的手机号
        }
      });
  
  },



  back2:function(){
    wx.navigateBack({
      delta:1
    })
  },
  toPageIndexp2:function(){
    //判断手机号与验证码是否匹配，匹配则进入下一个页面，不匹配则提示输入正确的验证码
    wx.navigateTo({
      url: '/pages/phone/successP/successP',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.username);
    this.setData({
      username:options.username
    })
    console.log(this.data.username);
    
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})